let doms = {
    aside: {
        nickname: document.querySelector("#nickname"),
        loginId: document.querySelector("#loginId")
    },
    txtMsg: document.querySelector("#txtMsg"),
    chatContainer: document.querySelector(".chat-container"),
    msgContainer: document.querySelector(".msg-container"),
    iconClose: document.querySelector(".iconfont.icon-close")
}

let zhi;
/**
 * 验证当前是否处于登录状态
 */
async function isLogoIn() {
    // let lp = getLocaltorage();
    zhi = await profile()
    // console.log(zhi);
    if (zhi.code !== 0) {
        alert("当前登录账号失效，请重新登录");
        location.href = "login.html"
        return;
    }
    SetAccount(zhi.data.loginId, zhi.data.nickname);
}
isLogoIn();
/**
 * 用于设置页面的账号id和名字
 * @param {String} loginId 传入账号id
 * @param {String} nickname 传入账号的名字
 */
function SetAccount(loginId, nickname) {
    doms.aside.nickname.innerText = nickname;
    doms.aside.loginId.innerText = loginId;
}

/**
 * 将一个时间戳转换成时间
 * @param {Date} date 传入一个时间戳 
 * @returns 返回转换后的数据
*/
CalculatTime(8370843297360);
function CalculatTime(date) {
    let zdate = new Date(date);
    // console.log(zdate);
    let Year = zdate.getFullYear();
    let Month = (zdate.getMonth() + 1).toString().padStart(2, 0);
    let Riqi = zdate.getDate().toString().padStart(2, 0);
    let Hours = zdate.getHours().toString().padStart(2, 0);
    let Minutes = zdate.getMinutes().toString().padStart(2, 0);
    let Seconds = zdate.getSeconds().toString().padStart(2, 0);
    return Year + "-" + Month + "-" + Riqi + " " + Hours + ":" + Minutes + ":" + Seconds;
}
// AddMessage({
//     content: "123",
//     createdAt: 1690133298360,
//     from: null,
//     to: null,
//     _id: "64bd63325393e37e7ade24ac",
// })
/**
 * 加载所有消息
 */
(async function setMessage() {
    let data = await getHistory();
    // console.log(data.data);
    data.data.forEach((item) => {
        AddMessage(item);
    })
    scrollBottom();
})()
/**
 * 给页面添加一条消息
 * @param {*} obj 传入服务器返回的数据
 */
function AddMessage(obj) {
    let divChatItem = document.createElement("div");
    if (obj.from) {
        divChatItem.classList.add("chat-item");
        divChatItem.classList.add("me");
    } else {
        divChatItem.classList.add("chat-item");
    }
    let img = document.createElement("img");
    img.classList.add("chat-avatar");
    img.src = obj.from ? "./asset/avatar.png" : "./asset/robot-avatar.jpg";
    let divAge = document.createElement("div");
    divAge.classList.add("chat-content");
    divAge.innerText = obj.content;
    let divDate = document.createElement("div");
    divDate.classList.add("chat-date");
    divDate.innerText = CalculatTime(obj.createdAt * 1);
    divChatItem.appendChild(img);
    divChatItem.appendChild(divAge);
    divChatItem.appendChild(divDate);
    doms.chatContainer.appendChild(divChatItem);
}
doms.msgContainer.addEventListener("submit", async function (e) {
    e.preventDefault();
    AddMessage({
        content: doms.txtMsg.value,
        createdAt: Date.now(),
        from: zhi.data.loginId,
        to: null,
        _id: "64bd63325393e37e7ade24ac",
    })
    let val = doms.txtMsg.value;
    doms.txtMsg.value = "";
    scrollBottom();
    let data = await sendChat(val);
    let obj = data.data;
    console.log(obj)
    obj.from = null;
    AddMessage(obj);
    scrollBottom();

    // console.log(doms.txtMsg.value)
})
/**
 * 将消息区域显示最后一个
 */
function scrollBottom() {
    doms.chatContainer.scrollTop = doms.chatContainer.scrollHeight;
}
/**
 * 点击叉叉
 */
function xxx() {
    removeLocaltorage();
    location.href = "login.html";
}
doms.iconClose.addEventListener("click", function () {
    xxx();
})
